import _Api from "@/api";
import { Message, MessageBox } from "element-ui";
import fileStream from "@/public/fileStream.js";
import waterfall from "async-es/waterfall";
import aes from "@/public/aes";
export default {
  // 客户端送货单or拣配单打印
  printPc(type, query) {
    const ENCRYPT_KEY = "lesso2128token11";
    waterfall([
      (callback) => {
        _Api
          .getPrintToken()
          .then(({ data }) => {
            let token = aes.encrypt(data, ENCRYPT_KEY);
            callback(null, token);
          })
          .catch((err) => {
            MessageBox.alert(err.data.message, {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure",
            });
          });
      },
      (token, callback) => {
        let params = {
          token: token,
          ...query,
        };
        _Api
          .getPrintPurOrdData(params)
          .then(({ data }) => {
            let printData = data.rows;
            callback(null, printData);
          })
          .catch((err) => {
            MessageBox.alert(err.data.message, {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure",
            });
          });
      },
      (printData, callback) => {
        fileStream
          .getFileInfo(type, printData)
          .then((data) => {
            ipcRenderer.send("IPCR_M_PRINT", {
              type: type,
              rows: printData,
              groupName: data.groupName,
              optype: "pr",
              url: data.url,
              isdefaultp: 1, //控制是否直接打印,0为选择打印、1为直接打印
            });
          })
          .catch((err) => {
            MessageBox.alert(err, {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure",
            });
          });
      },
    ]);
  },
};
